﻿using Api.Shared.Model;
using Api.Shared.Model.Yanji;
using Api.Shared.MyPlc.S7;
using Api.Shared.Sugar;
using Api.Shared.Utils;
using Api.Shared.Utils.Yanji;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;

namespace FitterTest.Controllers
{
    [Route("api/private/v1/[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    public class DeviceRuntimeController : ControllerBase
    {
        private readonly ISugarClientService helper;
        private readonly IS7ConnService s7;
        private readonly ApiData apiData;
        private readonly Db95DeviceHis db95DeviceHis;

        public DeviceRuntimeController(
            [FromKeyedServices(nameof(SqlName.sqlite))] ISugarClientService helper,
            IS7ConnService s7,
            ApiData apiData,
            Db95DeviceHis db95DeviceHis
        )
        {
            this.helper = helper;
            this.s7 = s7;
            this.apiData = apiData;
            this.db95DeviceHis = db95DeviceHis;
        }

        [HttpGet]
        public ActionResult<MyApiResponse<List<DeviceRunTime>>> GetDeviceRuntime([FromQuery] QueryInfo queryInfo)
        {
            //var time = await s7.S7Plc.ReadClassAsync<Db95DeviceHis>(95, 0);
            var time = db95DeviceHis;
            if (time == null)
            {
                time = new();
            }
            var a = new MyApiResponse<List<DeviceRunTime>>(true, GetDeivce(time));
            a.Total = GetDeivce(time).Count;
            return a;
        }

        private List<DeviceRunTime> GetDeivce(Db95DeviceHis time)
        {
            DeviceRunTime kr500 =
                new()
                {
                    Name = "Kuka机器人",
                    Time = DateTime.Now,
                    Status = apiData.Short1,
                    TodayTime = time.Kr500Time
                };

            DeviceRunTime kr16 =
                new()
                {
                    Name = "立式加工中心1",
                    Time = DateTime.Now,
                    Status = apiData.Short2,
                    TodayTime = time.Kr16Time
                };

            DeviceRunTime dizuo1 =
                new()
                {
                    Name = "立式加工中心2",
                    Time = DateTime.Now,
                    Status = apiData.Short3,
                    TodayTime = time.Dizuo1Time
                };

            DeviceRunTime dizuo2 =
                new()
                {
                    Name = "立式加工中心4",
                    Time = DateTime.Now,
                    Status = apiData.Short4,
                    TodayTime = time.Dizuo2Time
                };

            DeviceRunTime dizuo3 =
                new()
                {
                    Name = "卧式加工1",
                    Time = DateTime.Now,
                    Status = apiData.Short5,
                    TodayTime = time.Dizuo3Time
                };

            DeviceRunTime kj1 =
                new()
                {
                    Name = "卧式加工2",
                    Time = DateTime.Now,
                    Status = apiData.Short6,
                    TodayTime = time.Kj1Time
                };

            DeviceRunTime kj2 =
                new()
                {
                    Name = "卧式加工3",
                    Time = DateTime.Now,
                    Status = apiData.Short7,
                    TodayTime = time.Kj2Time
                };

            DeviceRunTime kj3 =
                new()
                {
                    Name = "卧式加工4",
                    Time = DateTime.Now,
                    Status = apiData.Short8,
                    TodayTime = time.Kj3Time
                };

            DeviceRunTime zhou =
                new()
                {
                    Name = "卧式加工5",
                    Time = DateTime.Now,
                    Status = apiData.Short9,
                    TodayTime = time.ZhouTime
                };

            DeviceRunTime dm =
                new()
                {
                    Name = "珩磨机",
                    Time = DateTime.Now,
                    Status = apiData.Short10,
                    TodayTime = time.ZhouTime,
                };

            List<DeviceRunTime> devices =
            [
                kr500,
                kr16,
                kj1,
                kj2,
                kj3,
                dizuo1,
                dizuo2,
                dizuo3,
                dm,
                zhou
            ];

            return devices;
        }
    }
}
